package com.shoes.service.impl;

import com.shoes.dao.UserDAO;
import com.shoes.model.ResponseDO;
import com.shoes.model.UserDO;
import com.shoes.service.UserService;
import com.shoes.utils.LoggerUtil;
import com.shoes.utils.ResultCode;
import com.shoes.utils.ShortMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Random;

/**
 * @author asen
 */
@Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
@Service("userService")
public class UserServiceImpl implements UserService {

    private UserDAO userDAO;

    @Override
    public ResponseDO getUserInfo(long userId, long toUid) throws Exception{
        ResponseDO result;
        UserDO user = userDAO.queryUserInfoById(toUid);
        if(user != null) {
            result = new ResponseDO(ResultCode.GLOBAL_SUCCESSFUL, user);
        } else {
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null);
        }

        return result;
    }

    @Override
    public ResponseDO getAuthCode(String phoneNumber, Integer sendCode) throws Exception {
        ResponseDO result;
        int authCode;
        if(sendCode == null) {
            authCode = new Random().nextInt(9000) + 1000;
        } else {
            authCode = sendCode;
        }
        String message = "验证码： "+ authCode + " 。(十分钟内有效)。";
        int returnCode = ShortMessage.sendMessage(phoneNumber, message);
        if(returnCode == 0) {
            result = new ResponseDO(ResultCode.GLOBAL_SUCCESSFUL, authCode);
        } else {
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null, "message return code: " + returnCode);
        }

        return result;
    }

    @Override
    public ResponseDO checkRegister(String phoneNumber) throws Exception {
        ResponseDO result;
        boolean registered = userDAO.queryIsExistByPhone(phoneNumber);
        if(!registered) {
            result = new ResponseDO(ResultCode.GLOBAL_SUCCESSFUL, null);
        } else {
            result = new ResponseDO(ResultCode.USER_ALREADY_EXISTS, null);
        }
        return result;
    }

    @Override
    public ResponseDO userLogin(String phoneNumber, String password) throws Exception {
        ResponseDO result;
        String psw = userDAO.queryPasswordByPhoneNumber(phoneNumber);
        if(psw == null) {
            return new ResponseDO(ResultCode.USER_ACCOUNT_NOT_EXIST, null);
        }
        if(psw.equals(password)) {
            Long userId = userDAO.queryUserIdByPhoneNumber(phoneNumber);
            result = new ResponseDO(ResultCode.GLOBAL_SUCCESSFUL, userId);
        } else {
            result = new ResponseDO(ResultCode.USER_PASSWORD_ERROR, null);
        }
        return result;
    }

    @Override
    public ResponseDO userRegister(String phoneNumber, String password) throws Exception {
        ResponseDO result;

        UserDO user = new UserDO();
        user.setMobilePhoneNumber(phoneNumber);
        user.setUserName(phoneNumber);
        user.setPassword(password);
        user.setGender(0);
        user.setBirthday(new Timestamp(631123200000L)); //默认时间:1990-01-01 00:00:00.0

        int count = userDAO.insertUser(user);
        if(count == 1) {
            result = new ResponseDO(ResultCode.GLOBAL_SUCCESSFUL, user.getUserId());
        } else {
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null);
        }

        return result;
    }

    @Override
    public ResponseDO userLogout(String userId) throws Exception {
        return null;
    }

    @Override
    public ResponseDO thirdPartyLogin(String type, String account, String token) throws Exception {
        ResponseDO result;


        return null;
    }

    @Override
    public ResponseDO thirdPartyRegister(String type, String account) throws Exception {
        ResponseDO result;

        return null;
    }

    @Override
    public ResponseDO changePassword(long userId, String oldPassword, String newPassword) throws Exception {
        ResponseDO result;

        String password = userDAO.queryPasswordByUserId(userId);
        if(password == null) {
            return new ResponseDO(ResultCode.USER_ACCOUNT_NOT_EXIST, null);
        }

        if(password.equals(oldPassword)) {
            boolean update = userDAO.updatePasswordByUserId(userId, newPassword);
            if(update) {
                LoggerUtil.logger.info("Update password success. UserId: " + userId);
                result = new ResponseDO(ResultCode.GLOBAL_SUCCESSFUL, null);
            } else {
                LoggerUtil.logger.info("Update password error. UserId: " + userId);
                result = new ResponseDO(ResultCode.GLOBAL_FAILED, null);
            }
        } else {
            result = new ResponseDO(ResultCode.USER_PASSWORD_CHECK_FAILED, null);
        }

        return result;
    }

    @Override
    public ResponseDO resetPassword(String phoneNumber, String newPassword) throws Exception {
        ResponseDO result;

        boolean update = userDAO.updatePasswordByMobilePhoneNumber(phoneNumber, newPassword);
        if(update) {
            LoggerUtil.logger.info("Update password success. phoneNumber: " + phoneNumber);
            result = new ResponseDO(ResultCode.GLOBAL_SUCCESSFUL, null);
        } else {
            LoggerUtil.logger.info("Update password error. phoneNumber: " + phoneNumber);
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null);
        }

        return result;
    }

    public UserDAO getUserDAO() {
        return userDAO;
    }
    @Autowired
    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }
}
